#include "widget.h"
#include "homepage.h"
#include "musicPage.h"
#include "magicPage.h"
#include "settingPage.h"

#include <QSettings>
#include <ElaTheme.h>
#include <ElaApplication.h>

Widget::Widget(QWidget *parent)
    : ElaWindow(parent)
{
    initWindow();
    initContent();
    initSetting();
    moveToCenter();
    setIsDefaultClosed(false);
    connect(this, &Widget::closeButtonClicked, this, &Widget::onCloseButtonClicked);
}

Widget::~Widget()
{

}

void Widget::onCloseButtonClicked()
{
    QSettings setting(settingPath_, QSettings::Format::IniFormat);
    setting.clear();
    setting.setValue("Theme", eTheme->getThemeMode() == ElaThemeType::ThemeMode::Dark ? "Dark" : "Light");
    setting.setValue("Mica", eApp->getIsEnableMica());
    setting.setValue("DisplayModeArea", getNavigationBarDisplayMode() == ElaNavigationType::NavigationDisplayMode::Auto ? "Auto" :
                                        getNavigationBarDisplayMode() == ElaNavigationType::NavigationDisplayMode::Compact ? "Compact" :
                                        getNavigationBarDisplayMode() == ElaNavigationType::NavigationDisplayMode::Maximal ? "Maximal" : "Minimal");

    setting.sync();
    closeWindow();
}

void Widget::initWindow()
{
    setUserInfoCardPixmap(QPixmap(":/include/Image/liu.jpg"));
    setUserInfoCardTitle("子非鱼");
    setUserInfoCardSubTitle("刘大侠出品，必属精品！");
    resize(1200, 740);
}

void Widget::initContent()
{
    homePage_ = new HomePage(this);
    musicPage_ = new musicPage(this);
    // magicPage_ = new magicPage(this);
    settingPage_ = new settingPage(this);

    addPageNode("HOME", homePage_, ElaIconType::House);
    addPageNode("MusicTool", musicPage_, ElaIconType::Music);
    // addPageNode("Secret", magicPage_, ElaIconType::Question);
    addFooterNode("Setting", settingPage_, settingKey_, 0, ElaIconType::GearComplex);

    connect(homePage_, &HomePage::musicCardNavigation, this, [=]() { this->navigation(musicPage_->property("ElaPageKey").toString()); });
    // connect(homePage_, &HomePage::magicCarNavigation, this, [=]() { this->navigation(magicPage_->property("ElaPageKey").toString()); });
}

void Widget::initSetting()
{
    QSettings setting(settingPath_, QSettings::Format::IniFormat);
    eTheme->setThemeMode(setting.value("Theme", "Light").toString() == "Light" ? ElaThemeType::ThemeMode::Light : ElaThemeType::ThemeMode::Dark);
    eApp->setIsEnableMica(setting.value("Mica", false).toBool());
    QString DisplayModeArea = setting.value("DisplayModeArea", "Auto").toString();
    setNavigationBarDisplayMode(DisplayModeArea == "Auto" ? ElaNavigationType::NavigationDisplayMode::Auto :
                                DisplayModeArea == "Compact" ? ElaNavigationType::NavigationDisplayMode::Compact :
                                DisplayModeArea == "Maximal" ? ElaNavigationType::NavigationDisplayMode::Maximal :
                                ElaNavigationType::NavigationDisplayMode::Minimal);
}
